{% extends 'base.html' %}
{% load static %}

{% block title %}资产管理 - Allen监控系统{% endblock %}

{% block extra_css %}
<link href="{% static 'css/dataTables.bootstrap5.min.css' %}" rel="stylesheet">
{% endblock %}

{% block content %}
<div class="container">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>资产管理</h2>
        <a href="{% url 'asset_create' %}" class="btn btn-primary">
            <i class="fas fa-plus"></i> 新增资产
        </a>
    </div>

    <!-- 筛选表单 -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-3">
                    <select name="category" class="form-select">
                        <option value="">所有分类</option>
                        {% for category in categories %}
                        <option value="{{ category.id }}" {% if selected_category|stringformat:"s" == category.id|stringformat:"s" %}selected{% endif %}>
                            {{ category.name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <select name="status" class="form-select">
                        <option value="">所有状态</option>
                        {% for status_value, status_label in status_choices %}
                        <option value="{{ status_value }}" {% if selected_status == status_value %}selected{% endif %}>
                            {{ status_label }}
                        </option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-4">
                    <input type="text" name="search" class="form-control" placeholder="搜索资产名称/编号/负责人" 
                           value="{{ search_query|default:'' }}">
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">筛选</button>
                </div>
            </form>
        </div>
    </div>

    <!-- 资产列表 -->
    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped" id="assetsTable">
                    <thead>
                        <tr>
                            <th>资产编号</th>
                            <th>名称</th>
                            <th>分类</th>
                            <th>状态</th>
                            <th>位置</th>
                            <th>负责人</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for asset in page_obj %}
                        <tr>
                            <td>{{ asset.asset_number }}</td>
                            <td>{{ asset.name }}</td>
                            <td>{{ asset.category.name }}</td>
                            <td>
                                <span class="badge {% if asset.status == 'in_use' %}bg-success
                                                  {% elif asset.status == 'idle' %}bg-warning
                                                  {% elif asset.status == 'maintenance' %}bg-info
                                                  {% else %}bg-danger{% endif %}">
                                    {{ asset.get_status_display }}
                                </span>
                            </td>
                            <td>{{ asset.location.name }}</td>
                            <td>{{ asset.responsible_person }}</td>
                            <td>
                                <a href="{% url 'asset_edit' asset.pk %}" class="btn btn-sm btn-primary">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <a href="{% url 'asset_delete' asset.pk %}" class="btn btn-sm btn-danger">
                                    <i class="fas fa-trash"></i>
                                </a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="7" class="text-center">暂无资产数据</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- 分页 -->
            {% if page_obj.has_other_pages %}
            <nav aria-label="Page navigation" class="mt-4">
                <ul class="pagination justify-content-center">
                    {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page=1">&laquo; 首页</a>
                    </li>
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}">上一页</a>
                    </li>
                    {% endif %}

                    {% for num in page_obj.paginator.page_range %}
                    {% if page_obj.number == num %}
                    <li class="page-item active">
                        <span class="page-link">{{ num }}</span>
                    </li>
                    {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ num }}">{{ num }}</a>
                    </li>
                    {% endif %}
                    {% endfor %}

                    {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.next_page_number }}">下一页</a>
                    </li>
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">末页 &raquo;</a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script src="{% static 'js/jquery.dataTables.min.js' %}"></script>
<script src="{% static 'js/dataTables.bootstrap5.min.js' %}"></script>
<script>
    $(document).ready(function() {
        $('#assetsTable').DataTable({
            "language": {
                "url": "{% static 'js/datatables-zh.json' %}"
            },
            "pageLength": 10,
            "ordering": true,
            "searching": false  // 禁用内置搜索，使用我们的自定义筛选
        });
    });
</script>
{% endblock %} 